package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/9/20
 * Time: 14:43
 */
public class ActivityPage extends Utils {
    public static String url = "http://101.42.27.228:8080/admin.html";

    public ActivityPage() {
        super(url);
    }


    /**
     * 从后台管理页面点击活动列表页面，活动状态信息可以正常显示
     */
    public void checkActivityList01() {
        locationSubpage("//*[@id=\"activitiesList\"]");
        // 检查活动标题
        String title = driver.findElement(By.xpath("//*[@id=\"activities\"]/div[1]/div[1]/h4")).getText();
        // 检查活动内容
        String content = driver.findElement(By.xpath("//*[@id=\"activities\"]/div[1]/div[1]/p")).getText();
        // 检查活动状态
        String status = driver.findElement(By.xpath("//*[@id=\"activities\"]/div[1]/div[2]/p/a")).getText();

        // 检测活动列表是否存在
        String actualText = driver.findElement(By.xpath("/html/body/div/h2")).getText();
        String expectText = "活动列表";
        assert expectText.equals(actualText);
        // 校验活动内容、活动标题、活动状态不为空
        assert !title.isEmpty();
        assert !content.isEmpty();
        assert !status.isEmpty();
    }
    /**
     * 输入url打开人员列表页面
     */
    public void checkUserList02() {

    }

    /**
     * 点击活动状态可以跳转到界面
     */
    public void checkJump() {
        driver.findElement(By.xpath("//*[@id=\"activities\"]/div[1]/div[2]/p/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        String curWindow = driver.getWindowHandle();
        Set<String> windows = driver.getWindowHandles();
        for (String window : windows) {
            if (window != curWindow) {
                driver.switchTo().window(window);
            }
        }
        // 通过校验标题内容来判断是否成功跳转
        driver.findElement(By.xpath("//*[@id=\"title\"]"));
        driver.close();

        driver.switchTo().window(curWindow);
    }

    /**
     * 从后台管理页面打开
     */
    public void checkCreateActivity() {
        locationSubpage("//*[@id=\"createActivity\"]");
        // 检查创建抽奖活动标题
        String title = driver.findElement(By.xpath("/html/body/div[1]/h2")).getText();
        // 检查活动名称输入框
        driver.findElement(By.xpath("//*[@id=\"activityName\"]"));
        // 检查活动描述输入框
        driver.findElement(By.xpath("//*[@id=\"description\"]"));
        // 检查圈选奖品按钮
        String btn1 = driver.findElement(By.xpath("//*[@id=\"buttonPrizes\"]")).getText();
        // 检查圈选人员按钮
        String btn2 = driver.findElement(By.xpath("//*[@id=\"buttonUsers\"]")).getText();
        // 检查创建活动按钮
        String btn3 = driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).getText();

        String expectTitle = "创建抽奖活动";
        String expectBtn1 = "圈选奖品";
        String expectBtn2 = "圈选人员";
        String expectBtn3 = "创建活动";

        assert title.equals(expectTitle);
        assert btn1.equals(expectBtn1);
        assert btn2.equals(expectBtn2);
        assert btn3.equals(expectBtn3);
    }

    public void clear() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.getElementById('createActivity').value = '';");
        js.executeScript("document.getElementById('activityName').value = '';");
    }
    /**
     * 所有信息都不填点击创建活动
     */
    public void createActivityFail01() {
        driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).click();
        String titleError = driver.findElement(By.xpath("//*[@id=\"activityName-error\"]")).getText();
        String textError = driver.findElement(By.xpath("//*[@id=\"description-error\"]")).getText();
        String expectTitleError = "请输入活动名称";
        String expectTextError = "请输入活动描述";
        assert textError.equals(expectTextError);
        assert titleError.equals(expectTitleError);
    }

    /**
     * 只填写活动名称点击创建活动
     */
    public void createActivityFail02() {
        driver.findElement(By.xpath("//*[@id=\"activityName\"]")).sendKeys("UI自动化");
        driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).click();
        String titleError = driver.findElement(By.xpath("//*[@id=\"activityName-error\"]")).getText();
        String textError = driver.findElement(By.xpath("//*[@id=\"description-error\"]")).getText();
        String expectTextError = "请输入活动描述";

        assert titleError.isEmpty();
        assert  expectTextError.equals(textError);
    }

    /**
     * 填写活动标题和描述
     */
    public void createActivityFail03() {
        driver.findElement(By.xpath("//*[@id=\"description\"]")).sendKeys("基于selenium的web自动化测试");
        driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();
        String expectMsg = "请至少选择一个奖品";

        assert actualMsg.equals(expectMsg);
    }

    /**
     * 活动标题+描述+圈选奖品
     */
    public void createActivityFail04() throws InterruptedException {
        driver.findElement(By.xpath("//*[@id=\"buttonPrizes\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"prize-19\"]")).click();
        // 设置奖品
        WebElement amount = driver.findElement(By.xpath("//*[@id=\"prizesContainer\"]/div[1]/input[2]"));
        amount.clear();
        amount.sendKeys("1");
        WebElement selectElement = driver.findElement(By.xpath("//*[@id=\"prizesContainer\"]/div[1]/select"));
        Select select = new Select(selectElement);
        select.selectByValue("SECOND_PRIZE");

        driver.findElement(By.xpath("//*[@id=\"prize-20\"]")).click();
        WebElement amount2 = driver.findElement(By.xpath("//*[@id=\"prizesContainer\"]/div[2]/input[2]"));
        amount2.clear();
        amount2.sendKeys("1");
        WebElement selectElement2 = driver.findElement(By.xpath("//*[@id=\"prizesContainer\"]/div[2]/select"));
        Select select2 = new Select(selectElement2);
        select2.selectByValue("FIRST_PRIZE");
        // 点击确认
        driver.findElement(By.xpath("//*[@id=\"prizesModal\"]/div/div[3]/button[2]")).click();

        // 点击创建活动
        driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();
        String expectMsg = "请至少选择一个人员, 人员数量应大于等于奖品总量";
        assert actualMsg.equals(expectMsg);
    }
    /**
     * 活动标题 + 描述 + 圈选奖品 + 圈选人员，但勾选人员数量少于奖品
     */
    public void createActivityFail05() {
        driver.findElement(By.xpath("//*[@id=\"buttonUsers\"]")).click();

        // 圈选人员
        driver.findElement(By.xpath("//*[@id=\"user-52\"]")).click();


        // 点击确认
        driver.findElement(By.xpath("//*[@id=\"usersModal\"]/div/div[2]/button[2]")).click();

        // 点击创建活动
        driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();
        String expectMsg = "创建失败！活动关联的奖品及人员数量设置异常";
        assert actualMsg.equals(expectMsg);
    }

    /**
     * 正常插入
     */
    public void createActivitySuccess() {
        driver.findElement(By.xpath("//*[@id=\"buttonUsers\"]")).click();

        // 圈选人员
        driver.findElement(By.xpath("//*[@id=\"user-51\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"user-50\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"user-49\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"user-48\"]")).click();

        // 点击确认
        driver.findElement(By.xpath("//*[@id=\"usersModal\"]/div/div[2]/button[2]")).click();

        // 点击创建活动
        driver.findElement(By.xpath("//*[@id=\"createActivity\"]")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();
        String expectMsg = "创建成功!";
        assert actualMsg.equals(expectMsg);

    }

}
